package com.leetcode.algorithm.y22.m06;

/**
 * 1779. 找到最近的有相同 X 或 Y 坐标的点
 * 
 * https://leetcode.cn/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/
 * 
 * @author jie.deng
 *
 */
class Question1779Solution01 {
	
	public int nearestValidPoint(int x, int y, int[][] points) {
		int min = Integer.MAX_VALUE;
		int minIdx = -1;
		for (int i = 0; i < points.length; i++) {
			int[] point = points[i];
			if (point[0] == x || point[1] == y) {
				int tmp = Math.abs(point[0] - x) + Math.abs(point[1] - y);
				if (tmp < min) {
					min = tmp;
					minIdx = i;
				}
			}
		}
		return minIdx;
	}
    
}